/*
 * GsmDatabase.java
 *
 * Created on Sekmadienis, 2006, Spalio 29, 10.29
 *
 * @author Eugenijus Medelis
 */

package org.mif.mobp.cellid;

import org.mif.mobp.core.coord.Coordinates;
import org.mif.mobp.core.coord.GeographicalCoordinates;
import java.util.Date;

// --- xml'o importai ---'
import java.io.File;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 

public class GsmDatabase {
    private String dbPath="cells.xml";//mobp/org/mif/mobp/cellid/cells.xml"; jei nenustatomas galima padaryti konstanta
    
    public GsmDatabase() {
    }
    
    /** Metodas iesko GsmCoordinates atitikmens GeographicalCoordiates duomenu bazeje
     **/
    public GeographicalCoordinates getCoordinates(Coordinates c){
        //System.out.println("==> "+c);
        GsmCoordinates tc = (GsmCoordinates)c;  // patikslinam tipa
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (GsmDatabase.class.getResourceAsStream(dbPath));
            doc.getDocumentElement ().normalize ();

            NodeList cellList = doc.getElementsByTagName("cell");
            Element cellElement = search(doc.getElementsByTagName("cell"), tc.cellId);    // ieskom reikiamos celes
            if (cellElement!=null){
                int bias = Integer.valueOf(getValue(cellElement.getElementsByTagName("radius"),0));
                
                NodeList coordList = cellElement.getElementsByTagName("coordinates");
		Element coordElement = (Element)coordList.item(0);
                Element latitudeElement = (Element)coordElement.getElementsByTagName("latitude").item(0);
                int latDeg = Integer.parseInt(getValue(latitudeElement.getElementsByTagName("degrees"),0));
                double latMin = Double.valueOf(getValue(latitudeElement.getElementsByTagName("minutes"),0)).doubleValue();
                String latDir = getValue(latitudeElement.getElementsByTagName("direction"),0);
                
                Element longitudeElement = (Element)coordElement.getElementsByTagName("longitude").item(0);
                int lonDeg = Integer.parseInt(getValue(longitudeElement.getElementsByTagName("degrees"),0));
                double lonMin = Double.valueOf(getValue(longitudeElement.getElementsByTagName("minutes"),0)).doubleValue();
                String lonDir = getValue(longitudeElement.getElementsByTagName("direction"),0);
                
                return new GeographicalCoordinates(tc.getDate(), bias, latDeg, latMin, latDir, lonDeg, lonMin, lonDir);
            }
            else
                return null;
        }
        catch (SAXParseException err) {
        System.out.println ("GsmDatabase XML Parsing error" + ", line " 
             + err.getLineNumber () + ", uri " + err.getSystemId ());
        System.out.println(" " + err.getMessage ());
        }
        catch (SAXException e) {
        Exception x = e.getException ();
        ((x == null) ? e : x).printStackTrace ();
        }
        catch (Throwable t) {
        t.printStackTrace ();
        }
        return null;
    }
    
    /** Metodas grazina NodeList index elemento, String reiksme
     */
    private String getValue(NodeList tempList, int index){
	Element tempElement = (Element)tempList.item(index);
        NodeList textFNList = tempElement.getChildNodes();
	return ((Node)textFNList.item(0)).getNodeValue();
    }
    
    /** Metodas iesko xml medyje elemento kurio cellId paduodamas kaip parametras
     */
    private Element search(NodeList cellList, byte cellId){
        byte cid=0;
        int s=0;
        Element cellElement=null;
        
        while (s!=cellList.getLength() && cid!=cellId){
            Node cellNode = cellList.item(s);
            if(cellNode.getNodeType() == Node.ELEMENT_NODE){
                cellElement = (Element)cellNode;
                cid=(byte)Integer.parseInt(getValue(cellElement.getElementsByTagName("cellId"),0));
            }
            s++;
        }
        if (cid==cellId)
            return cellElement;
        else
            return null;
    }
}
